package com.sxf.controller.blog.admin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.sxf.controller.SuperController;
import com.sxf.domain.blog.Comment;
import com.sxf.domain.blog.CommentMeta;
import com.sxf.domain.viewbean.CommentVO;
import com.sxf.formbean.CommentForm;
import com.sxf.mybatis.page.Pagination;
import com.sxf.querybean.CommentQuery;
import com.sxf.service.CommentService;

/**
 * 后台评论管理
 */
@Controller("adminCommentController")
@RequestMapping("blog/admin/comment")
public class CommentController extends SuperController {
	@Resource
	private CommentService commentService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		binder.registerCustomEditor(Date.class, dateEditor);
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}

	@RequestMapping("")
	public ModelAndView toComment() throws Exception {
		ModelAndView mav = new ModelAndView("blog/admin/comment");
		return mav;
	}

	@RequestMapping("/statisticsAjax")
	@ResponseBody
	public Map<String, Integer> statisticsAjax() throws Exception {
		Map<String, Integer> map = commentService.queryStatistic();
		return map;
	}

	@RequestMapping("/listAjax")
	@ResponseBody
	public Pagination<CommentVO> listAjax(
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status)
			throws Exception {
		CommentQuery query = new CommentQuery();
		if (StringUtils.hasText(page)) {
			try {
				query.setCurrentPage(Integer.parseInt(page));
			} catch (Exception e) {
			}
		}
		if (StringUtils.hasText(rows)) {
			try {
				query.setPageSize(Integer.parseInt(rows));
			} catch (Exception e) {
			}
		}
		if (StringUtils.hasText(status)) {
			query.setApproved(status);
		}
		Pagination<CommentVO> cPage = commentService.queryPage(query);
		if (cPage == null) {
			cPage = new Pagination<CommentVO>();
		}
		return cPage;
	}

	@RequestMapping("/toAddOrUpdate")
	public ModelAndView toAddOrUpdate(
			@RequestParam(value = "commentId", required = false) Long commentId,
			WebRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		if (commentId != null) {
			CommentQuery query = new CommentQuery();
			query.setId(commentId);
			CommentVO info = commentService.queryOne(query);
			mav.addObject("comment", info);
		}
		mav.setViewName("blog/admin/comment_edit");
		return mav;
	}

	@RequestMapping("/doAddOrUpdate")
	public ModelAndView doAddOrUpdate(@Valid @ModelAttribute CommentForm form,
			BindingResult result,
			@RequestParam(value = "status", required = false) String status,
			WebRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		if (form != null && form.getId() != null) {
			// 修改
			Comment comment = new Comment();
			BeanUtils.copyProperties(form, comment);
			comment.setDateGmt(comment.getDate());
			if (StringUtils.hasText(status)) {
				comment.setApproved(status);
			}
			commentService.update(comment);
		} else {
			// 添加
		}
		mav.setViewName("redirect:/blog/admin/comment");
		return mav;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam(required = true) Long commentId,
			WebRequest request) throws Exception {
		if (commentId != null) {
			Comment comment = new Comment();
			comment.setId(commentId);
			commentService.delete(comment);
			CommentMeta cm = new CommentMeta();
			cm.setCommentId(commentId);
			commentService.deleteCommentMeta(cm);
		}
		return "success";
	}
}
